<?php 

// First pull the list of Role IDs affected by Auto Hide.
$RoleIDs = GetRoleIDsWithPermission($Context, 'PERMISSION_AUTO_HIDE');

// Next count the deleted comments and inactive discussions by users
// in these roles.
$Count = 0;
if (count($RoleIDs) > 0){
   // Count the Discussions.
   $Query = $Context->ObjectFactory->NewContextObject($Context, 'SqlBuilder');
   $Query->SetMainTable('Discussion','a');
   $Query->AddJoin('User','b','UserID','a','AuthUserID','right join');
   $Query->AddSelect('DiscussionID','a','DiscussionCount','COUNT');
   $Query->AddWhere('a','Active','','0','=');
   $RoleID = array_shift($RoleIDs);
   if (count($RoleIDs) > 0){
      $Query->AddWhere('b','RoleID','',$RoleID,'=','and','','1','1');
      foreach ($RoleIDs as $RoleID){
         $Query->AddWhere('b','RoleID','',$RoleID,'=','or');
      }
      $Query->EndWhereGroup();
   }
   else{
      $Query->AddWhere('b','RoleID','',$RoleID,'=','and');
   }
   $result = $Context->Database->Select($Query,'AutoHide','GetModerationCount',
   	$Context->GetDefinition('AutoHide_ErrorCountingHiddenComments'));
   if ($Context->Database->RowCount($result) == 0){
      $Context->WarningCollector->Add(
      	$Context->GetDefinition('AutoHide_ErrorCountingHiddenComments'));
   }
   else{
      $Row = $Context->Database->GetRow($result);
      $Count = ForceInt($Row['DiscussionCount'],'0');
   }

   // Count the Comments.
   $Query = $Context->ObjectFactory->NewContextObject($Context, 'SqlBuilder');
   $Query->SetMainTable('Comment','a');
   $Query->AddJoin('User','b','UserID','a','AuthUserID','right join');
   $Query->AddSelect('CommentID','a','CommentCount','COUNT');
   $Query->AddWhere('a','Deleted','','1','=');
   // Reuse RoleID var from the first query.
   if (count($RoleIDs) > 0){
      $Query->AddWhere('b','RoleID','',$RoleID,'=','and','','1','1');
      foreach ($RoleIDs as $RoleID){
         $Query->AddWhere('b','RoleID','',$RoleID,'=','or');
      }
      $Query->EndWhereGroup();
   }
   else{
      $Query->AddWhere('b','RoleID','',$RoleID,'=','and');
   }
   $result = $Context->Database->Select($Query,'AutoHide','GetModerationCount',
	$Context->GetDefinition('AutoHide_ErrorCountingHiddenComments'));
   if ($Context->Database->RowCount($result) == 0){
      $Context->WarningCollector->Add(
      	$Context->GetDefinition('AutoHide_ErrorCountingHiddenComments'));
   }
   else{
      $Row = $Context->Database->GetRow($result);
      $Count += ForceInt($Row['CommentCount'],'0');
   }
}

// Include the final count on the panel.
if (isset($Panel)){
   $Panel->AddList($Context->GetDefinition('AutoHide_ModerationPanelListName'),'120');
   $Panel->AddListItem($Context->GetDefinition('AutoHide_ModerationPanelListName'), 
	$Context->GetDefinition('AutoHide_CommentModerationLink') . " (" . $Count . " new)", 
	GetUrl($Context->Configuration, 'extension.php', '', '', '', '',
		'PostBackAction=AutoHideList')); 
}

?>
